import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/call/controller/xindong_call_controller.dart';
import 'package:xindong/routes/call/dialog/xindong_call_action_button.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/xindong_avatar.dart';

// 悬浮 接听
class XinDongCallAnswerDialog {
  // ======== 单例
  // 工厂方法构造函数
  factory XinDongCallAnswerDialog() => _getInstance();

  // instance的getter方法，singletonManager.instance获取对象
  static XinDongCallAnswerDialog get instance => _getInstance();

  // 静态变量_instance，存储唯一对象
  static XinDongCallAnswerDialog? _instance;

  // 获取对象
  static XinDongCallAnswerDialog _getInstance() {
    _instance ??= XinDongCallAnswerDialog._internal();
    return _instance!;
  }

  // 私有的自定义命名式构造方法，通过它实现一个类;
  // 子类不能继承internal
  // 不是关键字，可定义其他名字
  XinDongCallAnswerDialog._internal() {
    //初始化(设置一些默认的)...
  }

  // 唯一标识
  final String tag = "kTagCallAnswer";

  Future<dynamic> show({required Function() onAccept, required Function() onReject}) async {
    // 等待初始化之后再调用
    if (XinDongAll.instance.smartDialogInitialized == false) {
      Future.delayed(const Duration(seconds: 2)).whenComplete(() async {
        show(onAccept: onAccept, onReject: onReject);
      });
      return;
    }
    return await SmartDialog.show(
      tag: tag,
      alignment: Alignment.topCenter,
      animationTime: const Duration(milliseconds: 360),
      animationType: SmartAnimationType.centerScale_otherSlide,
      backDismiss: false,
      clickMaskDismiss: false,
      useAnimation: true,
      bindPage: false,
      maskColor: Colors.transparent,
      // 点击事件将穿透遮罩
      usePenetrate: true,
      builder: (BuildContext context) {
        return XinDongGirlAnswerView(onAnswer: onAccept, onRefuse: onReject);
      },
    );
  }

  void hide() {
    try {
      SmartDialog.dismiss(status: SmartStatus.custom, tag: tag);
    } catch (error) {
      mCatchPrint(error);
    }
  }
}

class XinDongGirlAnswerView extends StatelessWidget {
  final Function() onAnswer;
  final Function() onRefuse;

  const XinDongGirlAnswerView({Key? key, required this.onAnswer, required this.onRefuse}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final controller = XinDongCallController.to;
    return Stack(
      children: [
        Positioned(
          top: statusBarHeight,
          left: kPaddingValue,
          right: kPaddingValue,
          child: Container(
            padding: const EdgeInsets.only(left: 15, right: 15),
            width: double.infinity,
            height: 90,
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.grey.shade800),
            child: Row(
              children: [
                XinDongAvatarBorder(controller.targetUser, avatarSize: 64, borderWidth: 2.0),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        controller.targetUser.getName,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.w500),
                      ),
                      const SizedBox(height: 4),
                      // Row(
                      //   children: [
                      //     ImageHelper.asset(AssetsRes.ICON_GOLD_COIN, width: 14, height: 14),
                      //     const SizedBox(width: 4),
                      //     Text(
                      //       "${controller.currentPrice}金币/分钟",
                      //       style: const TextStyle(color: Colors.white, fontSize: 12),
                      //     ),
                      //   ],
                      // ),
                      const SizedBox(height: 4),
                      Text(
                        "邀请您进行：${controller.callingTypeText}",
                        style: const TextStyle(fontSize: 12, color: Colors.white70, fontWeight: FontWeight.normal),
                      ),
                    ],
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    XinDongCallActionButton(
                      size: 46.0,
                      icon: ImageUtil.asset(AssetsRes.BUTTON_HANGUP),
                      onPressed: onRefuse,
                    ),
                    const SizedBox(width: 16),
                    XinDongCallActionButton(
                      size: 46.0,
                      icon: ImageUtil.asset(AssetsRes.BUTTON_ACCEPT),
                      onPressed: onAnswer,
                    ),
                  ],
                ),
              ],
            ),
          ),
        )
      ],
    );
  }
}
